package com.test.ipc;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Logger {
    public static boolean bOutLog = true;
    public static boolean bSaveLog = true;
    public static String sLogPath = "D:\\Workspace\\0Java\\java_ipc\\out\\log.txt";
    private static final String TAG = "Test";

    public static final String ANSI_RESET  = "\u001B[0m";
    public static final String ANSI_BLACK  = "\u001B[30m";    //黑色
    public static final String ANSI_RED    = "\u001B[31;1m";  //红色，高亮
    public static final String ANSI_GREEN  = "\u001B[32m";    //绿色
    public static final String ANSI_YELLOW = "\u001B[33m";    //黄色
    public static final String ANSI_BLUE   = "\u001B[34;1m";  //蓝色，高亮
    public static final String ANSI_PURPLE = "\u001B[35m";    //紫色
    public static final String ANSI_CYAN   = "\u001B[36m";    //深绿色
    public static final String ANSI_WHITE  = "\u001B[37m";    //白色


    public static void v() {
        Logger.v("");
    }
    public static void v(String str) {
        Logger.v(TAG, str);
    }
    public static void v(String tag, String str) {
        log(tag, str, ANSI_RESET);
    }


    public static void d() {
        Logger.d("");
    }
    public static void d(String str) {
        Logger.d(TAG, str);
    }
    public static void d(String tag, String str) {
        log(tag, str, ANSI_GREEN);
    }


    public static void i() {
        Logger.i("");
    }
    public static void i(String str) {
        Logger.i(TAG, str);
    }
    public static void i(String tag, String str) {
        log(tag, str, ANSI_BLUE);
    }


    public static void w() {
        Logger.w("");
    }
    public static void w(String str) {
        Logger.w(TAG, str);
    }
    public static void w(String tag, String str) {
        log(tag, str, ANSI_PURPLE);
    }


    public static void e() {
        Logger.e("");
    }
    public static void e(String str) {
        Logger.e(TAG, str);
    }
    public static void e(String tag, String str) {
        log(tag, str, ANSI_RED);
    }


    private static void log(String tag, String str, String colors) {
        String sLog  = str;
        String sLog2 = str;
        if (bOutLog || bSaveLog) {
            try {
                StackTraceElement[] a = new Throwable().getStackTrace();
                StackTraceElement ste = a[0];
                for (StackTraceElement anA : a) {
                    if (!anA.getMethodName().equals("log") &&
                            !anA.getMethodName().equals("v") &&
                            !anA.getMethodName().equals("d") &&
                            !anA.getMethodName().equals("i") &&
                            !anA.getMethodName().equals("w") &&
                            !anA.getMethodName().equals("e")) {
                        ste = anA;
                        break;
                    }
                }
                String  sClassName = ste.getClassName();
                String sMethodName = ste.getMethodName();
                int          iLine = ste.getLineNumber();
                String   sFileName = ste.getFileName();

                String sTmpClassName = "";
                int iPos = sClassName.lastIndexOf(".");
                if(iPos <=0 ) {
                    sTmpClassName = sClassName;
                } else {
                    sTmpClassName = sClassName.substring(iPos+1);
                }

                SimpleDateFormat df = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss:SSS]");
                String        sTime = df.format(new Date());
                sLog  = sTime + "[" + sFileName + ":" + iLine + " " + sTmpClassName + "::" + sMethodName + "] " + colors + tag + " " + str + ANSI_RESET;
                sLog2 = sTime + "[" + sFileName + ":" + iLine + " " + sTmpClassName + "::" + sMethodName + "] " + tag + " " + str;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            if (bOutLog) {
                System.out.println(sLog);
            }
            if (bSaveLog) {
                saveLog(sLog2+"\n");
            }
        }
    }

    private static void saveLog(String log) {
        try {
            File file = new File(sLogPath);
            //true，表示追加到文本
            FileOutputStream fos = new FileOutputStream(file, true);
            byte[] bytes = log.getBytes("UTF-8");
            fos.write(bytes);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
